package com.tdt.alarm.controller.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.tdt.alarm.R;
import com.tdt.alarm.model.SettingDBHelper;
import com.tdt.alarm.model.SettingModel;

public class Fragment4 extends Fragment {

	String[] items = { "Snooze Method", "Ringtones", "About" };

	Ringtone rt;
	RingtoneManager mRingtoneManager;
	TextView text;
	Button button1;
	Cursor mcursor;
	Intent Mringtone;
	String title;
	SettingDBHelper settingDB;
	SettingModel setting;
	RadioButton rdMathSlove;
	RadioButton rdRandomButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		mRingtoneManager = new RingtoneManager(getActivity());
		mcursor = mRingtoneManager.getCursor();
		title = RingtoneManager.EXTRA_RINGTONE_TITLE;
		settingDB = new SettingDBHelper(getActivity().getApplicationContext());
		setting = settingDB.getFirstSetting();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (rt != null && rt.isPlaying()) {
			rt.stop();
		} else {

		}
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// return inflater.inflate(R.layout.setting, container, false);

		View view = inflater.inflate(R.layout.setting, container, false);

		ListView listViewSetting = (ListView) view
				.findViewById(R.id.listViewSetting);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this.getActivity(), android.R.layout.simple_list_item_1, items);
		listViewSetting.setAdapter(adapter);
		listViewSetting.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				
				case 0:
					onClickRepeate();
					break;

				case 1:
					onClickRingtone(view);
					break;
				default:
				}
			}
		});

		return view;
	}

	public void onClickRingtone(View arg0) {
		// TODO Auto-generated method stub
		// Starts the intent or Activity of the ringtone manager, opens popup
		// box
		Mringtone = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);

		// specifies what type of tone we want, in this case "ringtone", can be
		// notification if you want
		Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
				RingtoneManager.TYPE_RINGTONE);

		// gives the title of the RingtoneManager picker title
		Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
				"Ringtone Picker !");

		// returns true shows the rest of the song on the device in the default
		// location
		Mringtone.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM,
				true);

		Uri uri = settingDB.getFirstSetting().getRingtone();
		// chooses and keeps the selected item as a uri
		if (uri != null) {
			Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
					uri);
		} else {
			Mringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
					(Uri) null);
		}

		startActivityForResult(Mringtone, 0);

	}

	public void onActivityResult(int requestCode, int resultCode,
			Intent Mringtone) {
		switch (resultCode) {
		/*
*/

		case Activity.RESULT_OK:
			// sents the ringtone that is picked in the Ringtone Picker Dialog
			Uri uri = Mringtone
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

			// send the output of the selected to a string
//			String test = uri.toString();
//			Toast.makeText(getActivity(), test, Toast.LENGTH_SHORT).show();
			setting.setRingtone(uri);
			settingDB.updateSetting(setting);
			stop();
			// the program creates a "line break" when using the "\n" inside a
			// string value
			// text.setText("\n " + test + "\n " + title);

			// prints out the result in the console window
			Log.i("Sample", "uri " + uri);


			// inserts another line break for more data, this times adds the
			// cursor count on the selected item
			// text.append("\n " + mcursor.getCount());

			// set default ringtone
			try {
				RingtoneManager.setActualDefaultRingtoneUri(getActivity(),
						resultCode, uri);
			} catch (Exception localException) {

			}
			break;

		}

	}

	// this method captures the ringtone from the selection and plays it in the
	// main activity
	private void play(Uri uri) {
		// TODO Auto-generated method stub
		if (uri != null) {

			// in order to play the ringtone, you need to create a new Ringtone
			// with RingtoneManager and pass it to a variable
			rt = mRingtoneManager.getRingtone(getActivity(), uri);
			rt.play();

		}
	}

	// stop ringtone play
	private void stop() {
		if (rt != null) {
			rt.stop();
		}
	}
	
	/**
	 * Show Snooze Method
	 */
	
	public void onClickRepeate() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("Snooze Method");
		// builder.setMessage("Are you sure you want to delete this entry?");
		LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
		View promptView = layoutInflater.inflate(R.layout.set_snooze_method, null);
		builder.setView(promptView);
		
		rdMathSlove = (RadioButton) promptView.findViewById(R.id.radioMathSlove);
		rdRandomButton = (RadioButton) promptView.findViewById(R.id.radioRandomButton);
		
		setting = settingDB.getFirstSetting();
		switch(setting.getSnoozeMethod()){
		case 0:
			rdMathSlove.setChecked(true);
			break;
		case 1:
			rdRandomButton.setChecked(true);
			break;
		default:
			rdMathSlove.setChecked(true);
			
		}
		
		builder.setPositiveButton(android.R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						if(rdMathSlove.isChecked()){
							setting.setSnoozeMethod(0);
						} else {
							setting.setSnoozeMethod(1);
						}
						settingDB.updateSetting(setting);
					}
				})
				.setNegativeButton(android.R.string.no,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// do nothing
							}
						})
				// .setIcon(android.R.drawable.ic_dialog_alert)
				.show();
	}

}